#include <gtest/gtest.h>
#include <iostream>
#include <unordered_map>

using std::cout;
using std::endl;

class MObj {
 public:
  int i;
  std::string name;
};

int maina ()
{
  std::vector<MObj> vec_m = {MObj{1, "asd"}, MObj{2, "lkj"}, MObj{3, "pou "}, MObj{8, "oiu"}};
  MObj &max = vec_m.front ();
  int maxInt = INT32_MIN;
  for (
	const auto &item
	  : vec_m)
	{
	  if (item.i > maxInt)
		{
		  max = item;
		  maxInt = item.i;
		}
	}
  std::cout << max.name << "\n";
  return 0;
}

void LambdaTest ()
{
  const auto add_fn = [] (int a, int b)
  { return a + b; };
  const auto add_res = add_fn (1, 3);
  cout << add_res << endl;
}

TEST(t1, hw)
{
  maina ();
  std::cout << "hw" << std::endl;
}

TEST(lamb, l1)
{
  LambdaTest ();
}
#include <iostream>
#include <vector>

// 函数返回vector中指定索引元素的指针
const int *getElementPointer (const std::vector<int> &myVector, size_t index)
{
  // 检查索引是否有效
  if (index < myVector.size ())
	{
	  // 返回指向元素的指针
	  return &myVector[index];
	}
  else
	{
	  // 返回空指针表示索引无效
	  return nullptr;
	}
}

TEST(vector, v_prt)
{
  std::vector<int> myVector = {1, 2, 3, 4, 5};

  // 获取元素的指针
  size_t index = 2;  // 例如，获取索引为2的元素的指针
  const int *ptr = getElementPointer (myVector, index);

  // 使用指针访问元素的值
  if (ptr)
	{
	  std::cout << "Element at index " << index << ": " << *ptr << std::endl;
	}
  else
	{
	  std::cout << "Invalid index" << std::endl;
	}

}
class ExampleCopy {
 private:
  int &ref;  // const 引用成员

 public:
  ExampleCopy (int value) : ref (value)
  {}
  int get_ref ()
  {
	return ref;
  }

  void set_ref (int x)
  {
	ref = x;
  }
  // 赋值操作符，但可能受到限制
  ExampleCopy &operator= (const ExampleCopy &other)
  {
	// 由于 ref 是 const，这里可能无法满足赋值的需求
	// other.ref 也是 const，无法修改
	// ...
	return *this;
  }
};
TEST(va_copy, assin)
{
  const int xx = 3;
  ExampleCopy ec (xx);
  cout << ec.get_ref () << endl;
  auto ec1 = ec;
  ec1.set_ref (4);
  cout << ec1.get_ref () << endl;

};
void map_insert (std::unordered_map<int, MObj *> &dic, MObj &mo)
{
  MObj *x = &mo;
  dic.insert ({mo.i, x});
};
TEST(map, m1)
{
  std::unordered_map<int, MObj &> mm_map;
  MObj m_obj = {1, "123"};
  mm_map.insert ({1, m_obj});
  auto mobj_copy = mm_map.at (1);
  cout << mobj_copy.name << endl;
}

TEST(map, m1_ptr)
{
  std::unordered_map<int, MObj *> mm_map;
  {
	MObj m_obj = {1, "123"};
	map_insert (mm_map, m_obj);
  }
  auto mobj_copy = *mm_map.at (1);
  cout << mobj_copy.name << endl;
}